package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.service.db.YxyKeChengService;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class KeChengStatusListController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository schoolLessonRepository;

    @Autowired
    YxyLessonsSchoolCatalogRepository catalogRepository;

    @Autowired
    YxyKeChengService yxyKeChengServiceImpl;

    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/status/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        Integer status = reqModel.status;
        Integer isVisible = reqModel.isVisible;
        Integer pageNum = reqModel.pageNum;
        String keyword = (StringUtils.isBlank(reqModel.keyword) ? "" : "%" + reqModel.keyword + "%");
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;

        Map<String, Object> map = yxyKeChengServiceImpl.list(schoolId, status, isVisible, pageNum, keyword, catalogId1, catalogId2);

        Integer allSize = 0;
        List<Map<String, Object>> list = new ArrayList<>();
        if (null != map) {
            allSize = Integer.parseInt(map.get("allSize") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }

        List<Pojo> returnList = new ArrayList<Pojo>();
        for (Map<String, Object> m : list) {
            String id = m.get("id") + "";
            String name = m.get("name") + "";
            String cover = m.get("cover") + "";
            String summary = m.get("summary") + "";
            String author = m.get("author") + "";
            String catalog_id1 = m.get("catalog_id1") + "";
            String catalog_id2 = m.get("catalog_id2") + "";
            Integer count = Integer.parseInt(m.get("count") + "");
            String duration = disDuration(Integer.parseInt(m.get("duration") + ""));
            String vv = m.get("vv") + "";
            Object object1 = m.get("create_date");
            Object object2 = m.get("last_modified_date");
            Object object3 = m.get("up_date");
            Object object4 = m.get("down_date");
            Object object5 = m.get("reject_date");
            Pojo pojo = new Pojo();
            pojo.id = id;
            pojo.name = name;
            pojo.author = author;
            pojo.count = count;
            pojo.cover = cover;
            pojo.duration = duration;
            pojo.summary = summary;
            pojo.vv = vv;
            if (null != object1) {
                pojo.createDate = DateToolUtils.format((Date) object1, "yyyy-MM-dd");
            }
            if (null != object2) {
                pojo.lastModifiedDate = DateToolUtils.format((Date) object2, "yyyy-MM-dd");
            }
            if (null != object3) {
                pojo.upDate = DateToolUtils.format((Date) object3, "yyyy-MM-dd");
            }
            if (null != object4) {
                pojo.downDate = DateToolUtils.format((Date) object4, "yyyy-MM-dd");
            }
            if (null != object5) {
                pojo.rejectDate = DateToolUtils.format((Date) object5, "yyyy-MM-dd");
            }
            YxyLessonsSchoolCatalog catalog1 = catalogRepository.findById(catalog_id1);
            YxyLessonsSchoolCatalog catalog2 = catalogRepository.findById(catalog_id2);
            if (catalog1 != null) {
                String catalogName1 = catalog1.getName();
                pojo.catalogName1 = catalogName1;
            }
            if (catalog2 != null) {
                String catalogName2 = catalog2.getName();
                pojo.catalogName2 = catalogName2;
            }
            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.list = returnList;
        respModel.allPage = allSize;
        respModel.pageNum = pageNum;
        return respModel;
    }

    public static class Pojo {
        public String id;
        public String name; // 课程名称
        public String summary; // 课程简介
        public String cover;// 课程封面
        public Integer count;// 总课时
        public String duration;// 总时长
        public String createDate;// 创建时间
        public String lastModifiedDate;// 创建时间
        public String upDate;// 上架时间
        public String downDate;// 下架时间
        public String rejectDate;// 拒绝时间
        public String author;// 贡献者
        public String catalogName1 = ""; // 一级分类名称
        public String catalogName2 = ""; // 二级分类名称
        public String vv = "0"; // 二级分类名称
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public Integer pageNum;
        public Integer status;
        public Integer isVisible;
        public String keyword;
        public String catalogId1;
        public String catalogId2;
    }

    public static class RespModel extends YxyApiRespModel {
        public List<Pojo> list;
        public long allPage;
        public Integer pageNum;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
